package com.situ.mall.controller.front;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.constant.MallConstant;
import com.situ.mall.entity.Cart;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.CartUtil;
import com.situ.mall.vo.CartItemVO;
import com.situ.mall.vo.CartVO;

@Controller
@RequestMapping(value="/user")
public class FrontUserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ICategoryService categoryService;//退出后进入主界面查询数据
	@Autowired
	private ICartService cartService;
	
	
	//获得登陆页面
	@RequestMapping(value="/getLoginPage.shtml")
	public String getUserPage() {
		return "user/login";
	}
	
	//登陆验证
	@RequestMapping(value="/login.shtml")
	@ResponseBody
	public ServerResponse login(String username,String password,HttpSession session,HttpServletRequest request,HttpServletResponse response2) {
		ServerResponse response = userService.login(username, password);
		//用户存在
		if(response.isSuccess()){
			session.setAttribute("", username);
			session.setAttribute("user", response.getData());
			
			
			//用户存在，同步cart表，删除cart_cookie数据
			CartVO cartVO = CartUtil.getCartVOFromCookie(request);
			if(cartVO != null){
				List<CartItemVO> cartItemVOList = cartVO.getCartItemVOList();
				Iterator<CartItemVO> iterator = cartItemVOList.iterator();
				while (iterator.hasNext()) {
					CartItemVO cartItemVO = iterator.next();
					//将CartItem转换为Cart
					Cart cart = new Cart();
					
					User user = (User)session.getAttribute("user");//从session拿到user
					Integer userId = user.getId();//拿到userId
					cart.setUserId(userId); //添加用户id
					cart.setProductId(cartItemVO.getProduct().getId());//添加商品id
					cart.setQuantity(cartItemVO.getAmount());//拿到商品数量
					cart.setChecked(cartItemVO.getIsChecked());//拿到是否勾选
					cartService.insert(cart);//添加数据
					
					Cookie cookie = new Cookie(MallConstant.CART_COOKIE, null);//拿到要销毁的cookie名字，销毁时间设置为0
					cookie.setMaxAge(0);
					cookie.setPath("/"); //拿到路径，浏览器看
					response2.addCookie(cookie);//设置完成
				}
			}
			
		}
		return response;
	}
	
	//退出登陆
	@RequestMapping(value="/loginOut.shtml")
	public String loginOut(HttpSession session,Model model) {
		session.removeAttribute("username");//销毁session
		session.invalidate();
		
		//重定向到主页面
		return "redirect:/index.shtml";
	}
	
	//获得注册页面
	@RequestMapping(value="/register.shtml")
	public String getRegisterPage() {
		return "user/register";
	}
	
	//注册功能
	@RequestMapping(value="/add.shtml")
	@ResponseBody
	public ServerResponse addRegister(User user) {
		return  userService.add(user);
	}
	
}
